//
//  CZOrginalView.swift
//  git
//
//  Created by 徐 鹏超 on 16/4/10.
//  Copyright © 2016年 徐 鹏飞. All rights reserved.
//

import UIKit
import SnapKit
let HomeCellMargin = 8

class CZOrginalView: UIView {
    
    var constant: Constraint?
    
    var orginalStatusModel : CZStatusModel?{
        
        didSet{
            // 在cell给他赋值的时候,orginalStatusModel已经有内容
            
            // 名字
            nameLabel.text = orginalStatusModel?.user?.screen_name
            // 微博内容
            contentLabel.text = orginalStatusModel?.text
            
            levelImageView.image = orginalStatusModel?.user?.levelImage
            
            verfityImageView.image = orginalStatusModel?.user?.verifiedImage
            
            sourceLabel.text = orginalStatusModel?.source_string
            
            
            timeLabel.text = orginalStatusModel?.created_string

            // 先把约束卸载
            constant?.uninstall()
            
            if let pic_urls = orginalStatusModel?.pic_urls where pic_urls.count > 0{
                
                printLog("显示配图视图")
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    constant = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                // 赋值
                pictureView.pic_urls = pic_urls
                
            }else{
                printLog("隐藏配图视图")
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    constant = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
            
            
        }
        
    }
    
    
    // 1
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 2.  添加视图和设置约束
    private func setupUI(){
        
        addSubview(photoImageView)
        addSubview(nameLabel)
        addSubview(levelImageView)
        addSubview(verfityImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 设置约束 距离左边一个边距,距离顶部一个边距,宽高
        photoImageView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left).offset(5)
            make.top.equalTo(self.snp_top).offset(10)
            
            make.width.height.equalTo(40)
        }
        // 约束 : 左边和 头像的右边有一定的间距;顶部和头像的顶部对其
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(HomeCellMargin)
            make.top.equalTo(photoImageView.snp_top)
        }
        
        // 约束: 左边距离nameLabel的右边一定间距;CenterY和nameLabel的centerY对其
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(HomeCellMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        // CenterX 和头像的 右边对其; CenterY和 头像的 底部
        verfityImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
        // 约束: 左边和头像的右边有一定的间距; 底部和头像的底部对其
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(HomeCellMargin)
            make.bottom.equalTo(photoImageView.snp_bottom)
        }
        
        
        // 约束: 左边和 时间的右边有一定的间距;底部和头像的底部对其
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(HomeCellMargin)
            make.bottom.equalTo(photoImageView.snp_bottom)
        }
        
        // 约束:
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(photoImageView.snp_bottom).offset(HomeCellMargin)
            make.right.equalTo(self.snp_right)
            
            // 微博微博的底部视图约束
//            make.bottom.equalTo(self.snp_bottom)
        }
        //约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(contentLabel.snp_bottom)
            // 给配图视图一个大小
            // 不要去计算它的大小,先给他一个固定的大小,显示出来,缩小我们的思路
            make.width.height.equalTo(100)
        }
        // 自动计算宽高是不可以的,因为没有告诉 self的底部在哪里吧!!!!
        self.snp_makeConstraints { (make) -> Void in
            constant = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }

    }
    
    //MARK:--懒加载
    private lazy var photoImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        return img
    }()
    //微博名字
    private lazy var nameLabel:UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        
        label.text = "微博名字"
        
        label.font = UIFont.systemFontOfSize(15)
        
        
        return label
    
    }()
    /// 等级
    private lazy var levelImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "common_icon_membership_level1")
        
        return img
        
    }()
    /// 认证图片
    private lazy var verfityImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_grassroot")
        
        return img
        
    }()
    /// 发布时间
    private lazy var timeLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "时间"
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.textColor = UIColor.orangeColor()
        
        
        return label
    }()
    
    /// 发布来源
    private lazy var sourceLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "来源"
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.textColor = UIColor.darkGrayColor()
        
        
        return label
    }()
    
    ///微博内容
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "微博内容"
        
        label.font = UIFont.systemFontOfSize(16)
        
        label.numberOfLines = 0
        
        return label
    }()
    
    

    private lazy var pictureView: CZPictureView = CZPictureView()
}
